package queryTree;

import java.util.ArrayList;

import optimization.Field;

/**
 * @author Van
 * 
 */
public class SimpleBoolExpr implements BooleanExpr{
	/** equal to * */
	public static final int EQ = 0;

	/** not equal to * */
	public static final int NE = 1;

	/** greater than * */
	public static final int GT = 2;
	
	/** lower than * */
	public static final int LT = 3;

	/** greater than or equal to * */
	public static final int GE = 4;
	
	/** lower than or equal to * */
	public static final int LE = 5;

	

	

	/*---------+
	 |          |
	 ----------*/
	//the boolean expression is 'att operator value'. Ex: att = value
	private Field attr;

	private String value;

	private int operator;

	public SimpleBoolExpr() {
		super();
	}
	public SimpleBoolExpr(Field attr, String value, int op) {
		super();
		if (op < EQ && op > GT) {
			throw new IllegalArgumentException("Illegal operation value: " + op);
		} else {
			this.attr = attr;
			this.value = value;
			this.operator = op;
		}
	}
	
	public SimpleBoolExpr(Field attr, String value) {
		this(attr, value, EQ);
	}

	
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o instanceof SimpleBoolExpr) {
			SimpleBoolExpr other = (SimpleBoolExpr) o;
			return attr.equals(other.getAttr())				 	
					&& (operator == other.getOperator())
					&& value.equals(other.getValue());
		}
		return false;
	}

	public int hashCode() {
		return attr.hashCode() + new Integer(operator).hashCode() + value.hashCode();
	}
	
	//check to see if this boolean expression conflicts with other boolean expression.
	//If AND --> false
	public boolean isOpposite(SimpleBoolExpr p)
	{
		if(attr.equals(p.getAttr()))
			if(value.equals(p.getValue())){
				if(operator == EQ) {
					if(p.getOperator() == NE || p.getOperator() == LT || p.getOperator() == GT) 
						return true;					
				} else if (operator == NE) {
					if(p.getOperator() == EQ) 
						return true;
				} else if (operator == LT) {
					if(p.getOperator() == EQ || p.getOperator() == GT || p.getOperator() == GE) 
						return true;
				} else if (operator == LE) {
					if(p.getOperator() == GT) 
						return true;
				} else if (operator == GE) {
					if(p.getOperator() == LT) 
						return true;
				} else if (operator == GT) {
					if(p.getOperator() == EQ || p.getOperator() == LT || p.getOperator() == LE) 
						return true;
				}
			} else
			{
				if(operator == EQ && p.getOperator()==EQ) return true;
			}
		return false;
	}
		
	
	public Field getAttr() {
		return attr;
	}

	public void setAttr(Field attr) {
		this.attr = attr;
	}

	public int getOperator() {
		return operator;
	}

	public void setOperator(int operator) {
		this.operator = operator;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void getAttrList(ArrayList<Field> l) {
		// TODO Auto-generated method stub
		l.add(attr);
	}
	
	
	public String toString() {
		return attr.item_name.toString() + " " + Common.getOperator(operator) + " " + value; 
	}
}

